package com.mouse.hxsj.service;

import com.mouse.damo.micro.*;
import com.mouse.haoi.service.CustomService;
import com.mouse.hxsj.entity.enums.MapPlace;
import com.mouse.hxsj.util.CommonUtil;
import com.mouse.hxsj.util.JsonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//试炼
public class ExerciseService {
    static CustomService customService = new CustomService();

    public static void main(String[] args) {
        Com com = new Com();
        com.setResourcePath("D:\\damo");
        boolean bind = com.bind(132790);
        System.out.println(bind);
        com.setDict(3, "试炼.txt");
        //
        start(com);
    }

    public static void start(Com com) {
        Press press = new Press(com);
        FindPic findPic = new FindPic(com);
        FindStr findStr = new FindStr(com);
        Mouse mouse = new Mouse(com);
        press.keyPress(Press.M);
        Sleep.sleep(200);
        int[] pic = findPic.findPic(0, 0, 961, 187,
                "login/场景地图.bmp", "203040", 0.8, 0);
        Sleep.sleep(200);
        mouse.mouseDoubleClick(pic[1] + 360, pic[2] + 504);
        Sleep.sleep(3000);
        mouse.mouseDoubleClick(pic[1] + 360, pic[2] + 504);
        Sleep.sleep(200);
        press.keyPress(Press.M);

        //完成 领取任务
        List<Integer> pointList = new ArrayList<>();
        boolean npcFlag = customService.loop(10, () -> {
            int[] pic2 = findPic.findPic(0, 0, 200, 187,
                    "login/试炼头像.bmp", "203040", 0.8, 0);
            if (pic2[0] != -1) pointList.addAll(Arrays.asList(pic2[1], pic2[2]));
            return pic2[0] != -1;
        });
        finish(mouse, pointList);
        Sleep.sleep(3000);
        //查看任务
        int[] strE = findStr.findStrE(0, 0, 1000, 800, "试炼完成", "fff2b6-000000", 0.9, 3);
        mouse.mouseClick(strE[1] + 30, strE[2] + 5, true);
        System.out.println(JsonUtil.toJson(strE));
        Sleep.sleep(1000);
        String mapStr = findStr.readStr(strE[1] - 200, 0, strE[1], 800,
                "ffffff-000000", "", 0.9, 1);
        press.keyPress(Press.ESC);
        MapPlace mapPlace = MapPlace.getByMap(mapStr);
        MapPlace.fly(com, mapPlace);
        String mapStrFinal=mapStr;
        boolean inJzFlag = customService.loop(10, () -> CommonUtil.getMap(com).getMap().equals(mapStrFinal));
        System.out.println(inJzFlag);
    }

    private static void finish(Mouse mouse, List<Integer> pointList) {
        mouse.mouseDoubleClick(pointList.get(0) + 54, pointList.get(1) + 212);
        Sleep.sleep(500);
        mouse.mouseDoubleClick(pointList.get(0) + 229, pointList.get(1) + 426);
    }
}
